<?
class Bb_vehicleModel extends Master_Model 
{
    function __construct()
    {
        parent::Model();
        //$this->obj =& get_instance();
        //$this->load->model('Master_Model', 'mdb');
        $this->table = 'bb_vehicle';
        $this->mdb->initdb();
    }
    
    // //////////////////////////////////////////////////////////////////////////
    // Function: findAll()
    //
    // Description: Retrieves and returns data listing from the database
    //
    // //////////////////////////////////////////////////////////////////////////
    function findAll($start = NULL, $count = NULL) {
        return $this->find(NULL, $start, $count);
    }
    
    //   function findById($key_value) {
        //      return $this->find(array('id' => '$key_value'));
    //   }
    
    function findByFilter($filter_rules, $start = NULL, $count = NULL) {
        return $this->find($filter_rules, $start, $count);
    }
    
    function find($filters = NULL, $start = NULL, $count = NULL) {
        
        $results = array();
        
        // ///////////////////////////////////////////////////////////////////////
        // Make a note of the current table record count
        // ///////////////////////////////////////////////////////////////////////
        $this->table_record_count = $this->db->count_all( 'bb_vehicle' );
        
        
        // Filter could be an array or filter values or an SQL string.
        $where_clause = '';
        if ($filters) {
            if ( is_string($filters) ) {
                $where_clause = $filters;
            }
            elseif ( is_array($filters) ) {
                // Build your filter rules
                if ( count($filters) > 0 ) {
                    foreach ($filters as $field => $value) {
                        $filter_list[] = " $field = '$value' ";
                    }
                    $where_clause = ' WHERE ' . join(' AND ', $filter_list );
                }
            }
            
        }
        
        $limit_clause = '';
        if ($start) {
            if ($count) {
                $limit_clause = " $start, $count ";
            }
            else {
                $limit_clause = " $start ";
            }
        }
        
        // Build up the SQL query string and run the query
        $sql = 'SELECT * FROM bb_vehicle ' . $where_clause . $limit_clause;
        
        $query = $this->db->query($sql);
        
        if ($query->num_rows() > 0) {
            // ////////////////////////////////////////////////////////////////////
            // NOTE: At this stage you could return the entire result set, like:
            // NOTE: ...return $query->result_array();
            // NOTE: ...The generated code loops through the result set to provide
            // NOTE: ...the oppurtunity to provide further customisations on the
            // NOTE: ...code (especially if you are generating in verbose mode).
            // ////////////////////////////////////////////////////////////////////
            
            foreach ($query->result_array() as $row)      // Go through the result set
            {
                // Build up a list for each column from the database and place it in
                // ...the result set
                
                $query_results['id']		 = $row['id'];
                $query_results['Title']		 = $row['Title'];
                $query_results['Showroom']		 = $row['Showroom'];
                $query_results['Image']		 = $row['Image'];
                $query_results['org_id']		 = $row['org_id'];
                
                $results[]		 = $query_results;
                
                
            }
            
        }
        
        return $results;
        
    }
    
    
    
    function get_by_oid() {
        
        $results = array();
        //$this->get_Org_id();
        
        //$query = $this->db->query("SELECT `Image`, `id`, `Title`, `Showroom` FROM `bb_vehicle` WHERE `org_id` = '$this->org_id'");
        $query = $this->db->query("SELECT `Image`, `id`, `Title`, `Showroom` FROM `bb_vehicle`");
        if ($query->num_rows() > 0) {
            foreach($query->result_array() as $k => $v){
                $results[$k] = $v;
            }
        } else {
            $results = FALSE;
        }
        return $results;
    }
    
    function get_by_id($idField) {
        
        $results = array();
        
        $query = $this->db->query("SELECT * FROM bb_vehicle WHERE id = '$idField' LIMIT 1");
        
        if ($query->num_rows() > 0) {
            $results = $query->row_array(); 
        }else{
            $results = false;
        }
        return $results;
    }
    
    
    function add( $data ) {
        $sql = $this->db->insert_string('bb_vehicle', $data);
        $query = $this->db->query($sql);
        return $this->db->insert_id();
    }

    function edit_image($id, $vdata) {
            $where = "id = ".$id;
            $sql = $this->db->update_string('bb_vehicle', $vdata, $where);
            $query = $this->db->query($sql);
    }

    
    function modify($id, $vdata) {
        if( ! isset($this->dbdata)){
            $this->dbdata = $this->get_by_id($id);
        }
        $newdata = new_value($this->dbdata, $vdata);
        if(count($newdata) != 0){
            $where = "id = ".$id;
            $sql = $this->db->update_string('bb_vehicle', $newdata, $where);
            $query = $this->db->query($sql);
        }
    }
    
    function delete_by_id($idField)
    {
        $sql = "DELETE FROM bb_vehicle WHERE id = '$idField' ";
        $query = $this->db->query($sql);
        return true;
    }
    
    public function init_vehicle($id = FALSE)
    {
        if($id !== FALSE){
            $this->dbdata = $this->get_by_id($id);
        }else{
            $fields = $this->db->list_fields('bb_vehicle');
            foreach ($fields as $field) $this->dbdata[$field] = '';
        }
        return $this->dbdata;        
    }
    
    
    // ================= END ==============================
    
    
	function get_Id() {
    return $this->id;	}
    
	function set_Id($id) {
    $this->id = $id;	}
    
	function get_Title() {
    return $this->Title;	}
    
	function set_Title($Title) {
    $this->Title = $Title;	}
    
	function get_Showroom() {
    return $this->Showroom;	}
    
	function set_Showroom($Showroom) {
    $this->Showroom = $Showroom;	}
    
	function get_Image() {
    return $this->Image;	}
    
	function set_Image($Image) {
    $this->Image = $Image;	}
    
    
    // Initialize all your default variables here
    // Function used to initilialise class variables.
    // NOTE: Not particularly useful unless you are using model persistence
    // NOTE: You could add default values here, but fields are generally set empty
    private function empty_vehicle()
    {
		$this->id = "";
		$this->Title = "";
		$this->Showroom = "";
		$this->Image = "";
		$this->org_id = "";
        
    }
    
}

?>